<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ServiceController
 *
 * @author nmthong
 */
class MobileController extends Controller{
    // Members
    /**
     * Key which has to be in HTTP USERNAME and PASSWORD headers 
     */
    Const APPLICATION_ID = 'ASCCPE';
 
    /**
     * Default response format
     * either 'json' or 'xml'
     */
    private $format = 'json';
    /**
     * @return array action filters
     */    
    
        public function actionGetCategory(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_category";
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_category = array();

                while(($row=$dataReader->read())!==false) {
                    $rows_category[]=$row;

                }
                $data=array("categories"=>$rows_category);
                $result=array('data'=>$data,'status'=>'ok');
                $string=json_encode($result);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }            
        }
        
        public function actionGetItems(){
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $query="SELECT * FROM os_item WHERE IdCategory=".$json->category_id;
                $command=$connection->createCommand($query);
                $dataReader=$command->query();
                $rows_item = array();

                while(($row=$dataReader->read())!==false) {
                    $rows_item[]=$row;

                }
                $data=array("items"=>$rows_item);
                $result=array('data'=>$data,'status'=>'ok');
                $string=json_encode($result);
                echo $string;
            }catch(Exception $e){
                $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($result);
            }            
        }   
        
        public function actionInsertOrder()
        {
            try{
                $post = Yii::app()->request->rawBody;
                $json=json_decode($post);
                $connection=Yii::app()->db;   // assuming you have configured a "db" connection
                $current_time=time();
                if(strcmp($json->discount_code,"")!=0){
                    $sql="SELECT discount_percent FROM os_discount WHERE discount_code=".$json->discount_code." and discount_start<".$current_time." and discount_end>".$current_time;
                    $command=$connection->createCommand($sql);
                    $dataReader=$command->query();
                    if(($row=$dataReader->read())!==false) {
                        $json->discount=$json->sum_price*(intval($row)/100);
                    }               
                }
                $sql="CALL SP_INS_ORDER(:name_order,:name_customer,:mobile_customer,:date_order,:discount,:sum_price,:description,:is_finish,@out)";                 
           
                $command=$connection->createCommand($sql);
                $command->bindParam(':name_order',$json->name_order,PDO::PARAM_STR);
                $command->bindParam(':name_customer',$json->name_customer,PDO::PARAM_STR);
                $command->bindParam(':mobile_customer',$json->mobile_customer,PDO::PARAM_STR);
                $command->bindParam(':date_order',$json->date_order,PDO::PARAM_STR);

                $command->bindParam(':discount',$json->discount,PDO::PARAM_STR);                
                $command->bindParam(':sum_price',$json->sum_price,PDO::PARAM_STR);
                $command->bindParam(':description',$json->description,PDO::PARAM_STR);
                $command->bindParam(':is_finish',$json->is_finish,PDO::PARAM_INT);
                $row_count=$command->execute();
                $data = $connection->createCommand("select @out as result;")->queryScalar();
                $json->order_id=$data + 0;
                //insert  items
                $array_orderitems=$json->array_items;
                foreach ($array_orderitems as &$value) {
                    $sql="CALL SP_INS_ORDERITEM(:order_id,:item_id,:price,:name_item,:number_item,:discount)";                 
                    $command=$connection->createCommand($sql);
                    $command->bindParam(":order_id",$data,PDO::PARAM_STR);
                    $command->bindParam(":item_id",$value->Id,PDO::PARAM_INT);
                    $command->bindParam(":price",$value->Price,PDO::PARAM_STR);
                    $command->bindParam(":name_item",$value->NameItem,PDO::PARAM_STR);
                    $command->bindParam(":number_item",$value->NumberOrder,PDO::PARAM_INT);
                    $command->bindParam(":discount",$value->Discount,PDO::PARAM_STR);                
                    $row_count=$command->execute();
            
                }
                $resul=array('data'=>$json,'status'=>'ok');
                $string=json_encode($resul);
                echo $string;
            }catch(Exception $e){
                $resul=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                 echo $string=json_encode($resul);
            }
                           

        }        
        
}

?>
